@extends('admin.master')

@section('top')
	<div class="top-breadcrumb pull-left">{!! Breadcrumbs::render('order', $type) !!}</div>
	<div class="top-btn-group pull-right">
		<a href="{{ route('admin.order.create', ['type'=>2]) }}" class="btn btn-primary">
			<i class="fa fa-plus" aria-hidden="true"></i> 添加订单
		</a>
	</div>
@endsection

@section('content')
<div class="section-search mb10">
	{!! Form::model($where, ['method'=>'get', 'class'=>'form-inline']) !!}
	<input type="hidden" name="type" value="{{ $request->input('type') }}">
	@if ($request->has('status'))
	<input type="hidden" name="status" value="{{ $request->input('status') }}">
	@endif
	<div class="form-group">
		{!! Form::text('word', null, ['class'=>'form-control len wd-search', 'id'=>'kw', 'placeholder'=>'客户名称或域名']) !!}
	</div>
	<div class="form-group">
		<label class="control-label">类型：</label>
		{!! Form::select('product', ['全部类型'] + config('system.product')[2], null, ['class'=>'form-control']) !!}
	</div>
	<div class="form-group">
		<div class="checkbox">
			<label>
				{!! Form::checkbox('has-designer', 1, null) !!} 设计师
			</label>
		</div>
	</div>
	<button type="submit" class="btn btn-primary">查询</button>
{!! Form::close() !!}
</div>
<div class="section-tab mb10">
	<ul class="nav nav-tabs">
		<li @if (!$request->has('status')) class="active" @endif>
			<a href="{{ route('admin.order.index', ['type'=>2]) }}">
				所有
				<i class="p-count">({{ $counts->sum() }})</i>
			</a>
		</li>
	@foreach($system['order_status'] as $key=>$value)
		<li @if ($request->input('status') == $key) class="active" @endif>
			<a href="{{ route('admin.order.index', ['type'=>2, 'status'=>$key]) }}">
				{{ $value }}
				<i class="p-count">({{ $counts->get($key, 0) }})</i>
			</a>
		</li>
	@endforeach
	</ul>
</div>
<div class="gridSection">
{!! Form::open(['url'=>'admin/order']) !!}
	<table class="table table-hover table-list order-tb">
		<thead>
			<tr>
				<th class="text-center">序号</th>
				<th>签单时间</th>
				<th>到期时间</th>
				<th>客户名称</th>
				<th>
					<div class="dropdown">
						<a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true">
							{{ request()->input('tag', '全部订单') }}
							<span class="caret"></span>
						</a>
						<ul class="dropdown-menu order-tag">
							@foreach(config('system.tag.order') as $key=>$tag)
								<li><a href="javascript:;">{{ $tag }}</a></li>
							@endforeach
						</ul>
					</div>
				</th>
				<th>合同号</th>
				<th>订单类型</th>
				<th>产品明细</th>
				<th class="text-center">年限</th>
				<th>设计师</th>
				<th>业务员</th>
				<th>签单金额</th>
				<th>到账</th>
				<th>尾款</th>
				<th>网站进度</th>
				<th class="text-right">操作</th>
			</tr>
		</thead>
		<tbody>
			@if (!$data->isEmpty())
				@foreach ($data as $value)
					<tr>
						<td class="text-center">{{ $i++ }}</td>
						<td>{{ $value->signed_at->format('Y-m-d') }}</td>
						<td>{{ $value->expired_at->format('Y-m-d') }}</td>
						<td>
						{{ $value->customer->name }}
						@if ($value->expedited)
							<span class="label label-warning">加急</span>
						@endif
						</td>
						<td>
							<a href="javascript:;" data-id="{{ $value->id }}" data-tag="{{ $value->tag }}" class="tag-edit">
							@if ($value->tag) 
								<span class="column-text text-warning">{{ $value->tag }}</span>
							@else
								<span class="text-success">未设置</span>
							@endif
							</a>
						</td>
						<td><span class="text-success"><b>{{ $value->contract_no }}</b></span></td>
						<td>
							<span class="label label-info">{{ $system['product'][$value->type][$value->product] or '' }}</span>
						</td>
						<td>{{ $value->product_detail }}</td>
						<td class="text-center">{{ $value->year }}</td>
						<td><span class="text-success">{{ $value->designer->name or '' }}</span></td>
						<td>{{ $value->saler->name or '' }}</td>
						<td><span class="text-success">{{ (int)$value->amount }}</span></td>
						<td><span class="text-success">{{ $value->amount - $value->tail_amount }}</span></td>
						<td>
							@if ($value->tail_amount > 0)
								<span class="text-danger">{{ (int)$value->tail_amount }}</span>
							@else
								<span class="text-muted">/</span>
							@endif
						</td>
						<td>
							<span class="text-success">{{ $system['order_status'][$value->status] }}</span>
						</td>
						<td class="text-right">
							<div class="toolbar text-nowrap">
								<a href="{{ route('admin.order.show', $value->id) }}" class="btn-link">详情</a>
								<span class="text-explode ">|</span>
								<a href="{{ route('admin.order.edit', $value->id) }}" class="btn-link">修改</a>
								@if (auth()->user()->can('renew'))
									<span class="text-explode ">|</span>
									<a href="{{ route('admin.renew.create', ['order_id'=>$value->id]) }}" class="btn-link">续费</a>
								@endif
								@if (auth()->user()->isAdmin())
									<span class="text-explode ">|</span>
									<a href="javascript:void(0);" data-id="{{ $value->id }}" class="btn-link delete-item">删除</a>
								@endif
							</div>
						</td>
					</tr>
				@endforeach
			@endif
		</tbody>
	</table>
	@if (!$data->isEmpty())
	<table class="table table-footer table-fixed">
		<tfoot>
			<tr>
				<td>
					<div class="pagination-box">
						<div class="pull-right">
							<div class="pagination-info">共有{{ $data->total() }}条， 每页显示：20条</div>
							{!! $data->appends(request()->except('page'))->render() !!}
						</div>
					</div>				
				</td>
			</tr>
		</tfoot>
	</table>
	@else
	<div class="simple-none-data text-center">
		<i class="fa fa-exclamation-circle text-size-20 text-muted" style="vertical-align: middle"></i> 
		<span class="text-size-14 inline-block ml5">没有查询到符合条件的记录</span>
	</div>
	@endif
{!! Form::close() !!}
</div>
@endsection

@section('foot')
<script type="text/javascript">

	// 标签选择
	$('.dropdown-menu > li > a').on('click', function() {
		var text = ($(this).text() != '全部订单') ? $(this).text() : '';
		changeQueryParameter('tag', text);
	});

	// tag设置
	$('.tag-edit').on('click', function() {
		var _this = $(this);
		var tag = _this.data('tag');

		var tagList = [];
		$('.order-tag > li:gt(0)').each(function(i) {
			tagList.push("<li><a href='javascript:;'>"+$(this).text()+"</a></li>");
		});
		tagList = "<ul class='tag-click'>" + tagList.join('') + "</ul>";

		swal({
			title: "请填写要设置的标签",
			text: tagList,
			type: "input",
			html: true,   
			showCancelButton: true,   
			closeOnConfirm: false,   
			showLoaderOnConfirm: true,
			animation: "slide-from-top",
			inputValue: tag,
		}, 
		function(inputValue) {   
			if (inputValue === false) return false;

			if (inputValue == tag) {
				swal.close();
				return false;
			}

			$.ajax({
				url: HOME_URL + '/admin/order/' + _this.data('id') + '/tag',
				data: {tag: inputValue},
				success: function(data) {
					if (data) {
						if (inputValue == '') {
							_this.data('tag', '').find('span').text('未设置').removeClass('text-warning').addClass('text-success');
						} else {
							_this.data('tag', inputValue).find('span').text(inputValue).removeClass('text-success').addClass('text-warning');
						}
						swal("成功!", "设置TAG标签成功", "success");
					} else {
						swal("失败!", "设置TAG标签失败", "error");
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					swal("错误!", XMLHttpRequest.responseText, "error");
				}
			});

			 return false;
		});
	});

	$('body').on('click', '.tag-click a', function() {
		$(this).parents('.sweet-alert').find(':text').val($(this).text());
	});
</script>	
@endsection